package general;

import java.util.HashMap;
import java.util.Map;

import oracle.adf.view.rich.component.rich.layout.RichPanelSplitter;
import oracle.adf.view.rich.component.rich.nav.RichCommandButton;
import oracle.adf.view.rich.context.AdfFacesContext;
import oracle.adf.view.rich.render.ClientEvent;


public class Splitter {
    RichPanelSplitter ps1;
    RichPanelSplitter ps2;
    RichCommandButton splitterBtn;

    private Map<String, Boolean> splitere;

    public Splitter() {
        super();
        splitere = new HashMap<String, Boolean>();
        splitere.put("restrans", false);
        splitere.put("ps1", true);
        splitere.put("ps2", true);
    }

    public void clearSplitter() {
        if (splitere != null)
            splitere.clear();
        else
            splitere = new HashMap<String, Boolean>();
    }

    public String actiuniSplittere() {
        System.out.println("am intrat in actiuni Splitter");
        RichPanelSplitter rps1 = ps1;
        RichPanelSplitter rps2 = ps2;
        boolean restrans = splitere.get("restrans");
        if (rps1.getSplitterPosition() != 224)
            rps1.setSplitterPosition(rps1.getSplitterPosition());
        if (rps2.getSplitterPosition() != 200)
            rps2.setSplitterPosition(rps2.getSplitterPosition());
        clearSplitter();
        if (!restrans) {
            splitere.put("ps1", true);
            rps1.setCollapsed(true);
            splitere.put("ps2", true);
            rps2.setCollapsed(true);
            splitere.put("restrans", true);
        } else if (restrans) {
            splitere.put("ps1", false);
            rps1.setCollapsed(false);
            splitere.put("ps2", false);
            rps2.setCollapsed(false);
            splitere.put("restrans", false);
        }
        //refresh panelSplitter si buton
        AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
        adfFacesContext.addPartialTarget(ps1);
        adfFacesContext.addPartialTarget(ps2);
        adfFacesContext.addPartialTarget(splitterBtn);
        return "";
    }

    public void actiuneSplittereListener(ClientEvent cl) {
        System.out.println("*****************actiuneSplittereListener");
        RichPanelSplitter rps1 = ps1;
        RichPanelSplitter rps2 = ps2;
        boolean collapsed1 = rps1.isCollapsed();
        boolean collapsed2 = rps2.isCollapsed();
        if (collapsed1) {
            if (splitere.get("ps2") == false) {
                splitere.put("ps2", true);
            } else {
                if (splitere.get("ps2") == true) {
                    splitere.put("ps2", false);
                }
            }
        }
        if (collapsed2) {
            if (splitere.get("ps1") == false) {
                splitere.put("ps1", true);
            } else {
                if (splitere.get("ps1") == true) {
                    splitere.put("ps1", false);
                }
            }
        }
        boolean get = splitere.get("restrans");
        if (collapsed1 == collapsed2) {
            if (collapsed1 == true)
                if (get == false)
                    splitere.put("restrans", true);
                else if (get == true)
                    splitere.put("restrans", true);
        }
        if (rps1.getSplitterPosition() == 0 && rps2.getSplitterPosition() == 0)
            splitere.put("restrans", true);
        if (rps1.getSplitterPosition() > 0 && rps2.getSplitterPosition() > 0 && get == true && !collapsed1 &&
            !collapsed2)
            splitere.put("restrans", false);
        if (rps1.getSplitterPosition() > 0 && collapsed2)
            splitere.put("restrans", true);
        if (rps2.getSplitterPosition() > 0 && collapsed1)
            splitere.put("restrans", true);
        AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
        adfFacesContext.addPartialTarget(splitterBtn);
    }


    public void setSplitterBtn(RichCommandButton splitterBtn) {
        this.splitterBtn = splitterBtn;
    }

    public RichCommandButton getSplitterBtn() {
        return splitterBtn;
    }

    public void setSplitere(Map<String, Boolean> splitere) {
        this.splitere = splitere;
    }

    public Map<String, Boolean> getSplitere() {
        return splitere;
    }


    public void setPs1(RichPanelSplitter ps1) {
        this.ps1 = ps1;
    }

    public RichPanelSplitter getPs1() {
        return ps1;
    }

    public void setPs2(RichPanelSplitter ps2) {
        this.ps2 = ps2;
    }

    public RichPanelSplitter getPs2() {
        return ps2;
    }
}
